package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo5 {
    public static void main(String[] args) {
        /**
         * 查看王克晶所带班级的信息
         * 列出老师名字，工资，职称以及班级的名字，所在楼层
         */
        try (
                Connection con = DBUtil.getConnection();
                ){
            Statement statement = con.createStatement();
            String sql = "SELECT t.name,t.salary,t.title,c.name,c.floor " +
                         "FROM teacher t " +
                         "JOIN class c ON t.id=c.teacher_id "+
                         "WHERE t.name='王克晶' ";
            System.out.println(sql);
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String tName = rs.getString(1);
                int tSalary = rs.getInt(2);
                String tTitle = rs.getString(3);
                String cName = rs.getString(4);
                int cFloor = rs.getInt(5);
                System.out.println(tName+" "+tSalary+" "+tTitle+" "+cName+" "+cFloor);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
